using MathTutorPro.DAL;
using MathTutorPro.Data.Entities;
using MathTutorPro.Data.DAL;
using MathTutorPro.Services.Implementations;
using MathTutorPro.Services.Interfaces;
using MathTutorPro.Utilities;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using MathTutorPro.Data;
using Microsoft.EntityFrameworkCore.Migrations;
using MathTutorPro.Hubs;
using Microsoft.OpenApi.Models;
using Org.BouncyCastle.Pqc.Crypto.Lms;

namespace MathTutorPro
{
    public class Program
    {
        public static void Main(string[] args)
        {

            var builder = WebApplication.CreateBuilder(args);

            // 添加数据库上下文
            var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
            //builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(connectionString));
            //builder.Services.AddDbContext<AppDbContext>(options =>
            //{
            //    options.UseSqlServer(connectionString);

            //    // 替换 MigrationsSqlGenerator
            //    options.ReplaceService<IMigrationsSqlGenerator, CustomMigrationsSqlGenerator>();
            //});
            builder.Services.AddDbContext<AppDbContext>(option =>
            {
                option.UseSqlServer(connectionString);
                option.ReplaceService<IMigrationsModelDiffer, MigrationsModelDifferWithoutForeignKey>();
            });
            // 配置 Identity
            builder.Services
                .AddIdentity<AppUser, IdentityRole>(options =>
                {
                    options.Password.RequireNonAlphanumeric = false;  // 不需要非字母数字字符
                    options.Password.RequireUppercase = false;        // 需要大写字母
                    options.Password.RequiredLength = 6;            // 最小长度
                    options.Password.RequireDigit = true;           // 需要数字


                })
                .AddEntityFrameworkStores<AppDbContext>()
                .AddDefaultTokenProviders();

            builder.Services.AddSwaggerGen(options =>
            {
                // 配置 Swagger 文档基本信息
                options.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title = "MathTutorPro API",
                    Version = "v1"
                });

                // 配置 JWT 认证方案
                options.AddSecurityDefinition("JWT", new OpenApiSecurityScheme
                {
                    Description = "JWT 认证头（Bearer 方案）",
                    Name = "Authorization",        // 请求头名称
                    In = ParameterLocation.Header, // Token 位置（Header）
                    Type = SecuritySchemeType.Http,
                    Scheme = "bearer",             // 认证方案为 "bearer"
                    BearerFormat = "JWT"           // 可选：明确说明 Token 格式
                });

                // 全局启用 JWT 认证（所有接口默认需要认证）
                options.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "JWT"
                            }
                        },
                        Array.Empty<string>() // 不需要指定 Scope
                    }
                });

            });

            // 添加认证和授权
            // JWT 认证
            builder.Services.AddAuthentication(options =>
            {
                // 设置默认认证方案为 JWT
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    // 是否验证 Issuer（颁发者）
                    ValidateIssuer = true,
                    // 合法的 Issuer（需与 appsettings.json 中的配置一致）
                    ValidIssuer = builder.Configuration["Jwt:Issuer"],

                    // 是否验证 Audience（接收者）
                    ValidateAudience = true,
                    // 合法的 Audience
                    ValidAudience = builder.Configuration["Jwt:Audience"],

                    // 是否验证密钥
                    ValidateIssuerSigningKey = true,
                    // 密钥（从配置中读取并转换为字节数组）
                    IssuerSigningKey = new SymmetricSecurityKey(
                        Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])
                    ),

                    // 是否验证 Token 有效期
                    ValidateLifetime = true
                };
            });
            builder.Services.AddAuthorization();

            // 添加 SignalR
            builder.Services.AddSignalR();

            // 添加 CORS 策略
            builder.Services.AddCors(options =>
            {
                options.AddPolicy("AllowVueApp", builder =>
                {
                    builder.WithOrigins("http://localhost:9528") // 前端地址
                           .AllowAnyHeader()
                           .AllowAnyMethod()
                           .AllowCredentials(); // 若前端需要发送凭证（如 cookies）
                });
            });


            // 注册仓储
            builder.Services.AddScoped<ClassDal>();
            builder.Services.AddScoped<ClassDetailDal>();
            builder.Services.AddScoped<GroupChatDal>();
            builder.Services.AddScoped<GroupChatMessageDal>();
            builder.Services.AddScoped<GroupChatMemberDal>();
            // 注册服务
            builder.Services.AddScoped<IClassService, ClassService>();
            builder.Services.AddScoped<IClassDetailService, ClassDetailService>();
            builder.Services.AddScoped<IGroupChatService, GroupChatService>();


            builder.Services.AddControllers();
            builder.Services.AddScoped<JwtHelper>();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            //builder.Services.AddSwaggerGen();
            


            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                //app.UseSwaggerUI();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "MathTutorPro API v1");
                });
            }

            app.UseRouting();

            app.UseHttpsRedirection();


            // 启用 CORS（注意顺序：在 Routing 之后，Authorization 之前）
            app.UseCors("AllowVueApp");

            // 必须按顺序配置！
            app.UseAuthentication(); // 先认证
            app.UseAuthorization();  // 后授权

            app.MapHub<GroupChatHub>("/Hubs/GroupChatHub"); // SignalR Hub 路由


            app.MapControllers();

            app.Run();
        }
    }
}
